package com.fzrskj.cloud.free.user.utils.diy;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.enums.CartTypeEnum;
import com.fzrskj.cloud.free.core.enums.PriceTypeEnum;
import com.fzrskj.cloud.free.core.enums.ProductTypeEnum;
import com.fzrskj.cloud.free.core.tool.CDateTimeTool;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.dic.utils.DicPriceDiyUtils;
import com.fzrskj.cloud.free.user.bean.UserCart;
import com.fzrskj.cloud.free.user.bean.UserDiyViewBean;
import com.fzrskj.cloud.free.user.utils.UserCartUtils;
import com.fzrskj.cloud.free.user.utils.UserDiyUtils;
import com.fzrskj.cloud.free.user.utils.UserOrderUtils;
import com.fzrskj.cloud.free.user.bean.UserAccountView;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import com.winiis.component.common.utils.other.MapOperateUtils;
import ms.core.object.CObject;
import org.apache.commons.collections.map.HashedMap;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * UserDiyUpgradeUtils.
 *
 * @author zj.
 *         Created on 2019/9/29 0029.
 */
public class UserDiyUpgradeUtils extends CObject {
    /**
     * 添加升级购物车.
     * @param userId
     * @param id
     * @param orderParam
     * @return
     */
    public Boolean addUpgradeUserCart (Integer userId, Integer id, Map<String, Object> orderParam)  {
        Map<String, Object> cdt = new HashedMap();
        cdt.put("id", id);
        UserDiyUtils utls = new UserDiyUtils();
        List<UserDiyViewBean> rets = utls.userQuery(cdt,"id desc",1,1);
        if (rets.isEmpty()) {
            this.setErrCode("DIY BUSINESS NOT FOUND", "业务数据未找到");
            return false;
        }
        UserDiyViewBean viewBean = rets.get(0);

        //判断该实例有没有正在处理中的订单
        Map<String, Object> repeatUserCartCdt = new HashedMap();
        repeatUserCartCdt.put("productClass", ProductTypeEnum.DIY.getTag());
        repeatUserCartCdt.put("productCode", viewBean.getProductCode());
        repeatUserCartCdt.put("businessId", id);
        UserCartUtils cartUtils = new UserCartUtils();
        Integer existUserCart = cartUtils.queryUserCartExistCount(repeatUserCartCdt);
        if (existUserCart > 0) {
            this.setErrCode("SAME BUSINESS IN USER CART", "该业务购物车中有未处理的订单");
            return false;
        }

        //判断
        Map<String, Object> repeatUserOrder = new HashedMap();
        repeatUserOrder.put("productClass", ProductTypeEnum.DIY.getTag());
        repeatUserOrder.put("productCode", viewBean.getProductCode());
        repeatUserOrder.put("inStatus", "N,C");
        repeatUserOrder.put("instanceId", "\"id\":" + String.valueOf(id));
        UserOrderUtils orderUtils = new UserOrderUtils();
        Integer orderExistNumber = orderUtils.queryOrderExistCount(repeatUserOrder);
        if (orderExistNumber > 0) {
            this.setErrCode("SAME BUSINESS IN USER ORDER", "该业务存在未处理的订单");
            return false;
        }

        UserAccountView accountView = UserAccountUtils.getUserAccountView(userId);

        //获取升级费用
        DicPriceDiyUtils dicPriceDiyUtils = new DicPriceDiyUtils();
        Map<String, Object> diyParams = MapOperateUtils.getMap(orderParam, "diy_params");
        String result = dicPriceDiyUtils.getUpgradePrice(userId, id, diyParams);
        net.sf.json.JSONObject priceResult = CJsonTool.getJSONObject(result, "data", null);
        if (null == priceResult) {
            this.setErrCode("GET UPGRADE PRICE ERROR", "获取升级价格错误");
            return false;
        }

        UserCart userCart = new UserCart();
        userCart.setApplyTime(CDateTimeTool.daysBetween(new Date(), viewBean.getEndTime()));
        userCart.setCartNo(cartUtils.createCartNO());
        userCart.setCartTime(new Date());
        userCart.setCartType(CartTypeEnum.UPDATE.getType());
        userCart.setTotalPrice(priceResult.getDouble("totalPrice"));
        userCart.setPrice(priceResult.getDouble("payPrice"));
        userCart.setPriceType(PriceTypeEnum.DAY.getFlag());
        userCart.setProductClass(ProductTypeEnum.DIY.getTag());
        userCart.setProductCode(viewBean.getProductCode());
        userCart.setProductParam(JSONObject.toJSONString(orderParam));
        userCart.setRegType(ProductTypeEnum.DIY.getTag());
        userCart.setUserId(userId);
        userCart.setUserName(accountView.getUserName());
        userCart.setBusinessId(String.valueOf(id));
        userCart.setAgentId(CloudContextUtils.getAgentId());

        int ret=cartUtils.addUserCartRecord(userCart);

        if (ret < 1) {
            this.setErrCode(cartUtils.getErrCode(), cartUtils.getMsg());
            return false;
        }

        return true;
    }
}
